package com.bw.liveplay.common.mvvm.viewmodel

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.Toast
import com.bw.liveplay.R
import com.bw.liveplay.common.entity.BaseEntity
import com.bw.liveplay.common.entity.DissEntity
import com.bw.liveplay.common.entity.HomeFeedEntity
import com.bw.liveplay.common.entity.LoveEntity
import com.bw.liveplay.common.mvvm.adapter.HomeListAdapter
import com.bw.liveplay.common.mvvm.mondel.HomeListModel
import com.bw.liveplay.common.mvvm.view.CommentActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author KK
 * @description:
 * @date :2021/11/10 13:19
 *
 */
class HomeListViewModel : BaseViewModel<HomeListModel>(),OnItemChildClickListener{
    private var pageCount : Int = 12
    public val adapter:HomeListAdapter = HomeListAdapter()
    private var loveFlag = false
    private var caiFlag = false
    var homeFeedEntity: HomeFeedEntity ?=null

    //返回值
    override fun result(t: BaseEntity) {
        if(t is HomeFeedEntity){

            homeFeedEntity = t as HomeFeedEntity
            adapter.setNewInstance(homeFeedEntity!!.data!!.data)
        }
        if(t is LoveEntity){
            showMsg("喜欢成功")
        }
        if(t is DissEntity){
            showMsg("踩成功")
        }
    }

    override fun initData() {
        adapter.setOnItemChildClickListener(this)
        m.requestHomeList(0,"all",pageCount,0)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        var love = view.findViewById<ImageView>(com.bw.liveplay.R.id.item_homelist_love)
        var cai = view.findViewById<ImageView>(com.bw.liveplay.R.id.item_homelist_cai)
        when(view.id){
            com.bw.liveplay.R.id.item_homelist_love->{
                if(loveFlag){
                    love.setImageResource(com.bw.liveplay.R.drawable.zan_false)
                    loveFlag=false

                }else{
                    love.setImageResource(com.bw.liveplay.R.drawable.zan_true)
                    //喜欢一条段子
                    m.requestLove(1615197740457,0)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(this)
                    loveFlag=true
                }
            }
            com.bw.liveplay.R.id.item_homelist_cai->{
                if(caiFlag){
                    cai.setImageResource(com.bw.liveplay.R.drawable.cai_false)
                    caiFlag=false

                }else{
                    cai.setImageResource(com.bw.liveplay.R.drawable.cai)
                    //踩一条段子
                    m.requestDiss(1615197740457,0)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(this)
                    caiFlag=true
                }
            }
            R.id.item_homelist_ll->{
                var bundle:Bundle = Bundle()
                bundle.putString("head",homeFeedEntity!!.data!!.data!!.get(position).author!!.avatar)
                bundle.putString("username",homeFeedEntity!!.data!!.data!!.get(position).author!!.name)
                bundle.putString("coment",homeFeedEntity!!.data!!.data!!.get(position).feeds_text)
                bundle.putString("gsy",homeFeedEntity!!.data!!.data!!.get(position).url)
                startPage(CommentActivity::class.java,bundle)
            }
        }
    }


}